package idiet.view;


import idiet.Activator;
import idiet.bean.FoodIngredient;
import idiet.bean.FoodMaterial;
import idiet.service.FoodIngredientService;
import idiet.service.FoodMaterialService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class ViewPartFoodMaterial extends ViewPart{

	public static final String ID = "iDiet.view.food.material";
	private Logger logger=Logger.getLogger(ViewPartFoodMaterial.class);
	private Text textFoodName;
	private Table table;
	private CheckboxTableViewer checkboxTableViewer;
	private TableViewer tableViewer;
	private List<FoodMaterial> foodMaterials=new ArrayList<FoodMaterial>();
	private List<FoodIngredient> foodIngredients=new ArrayList<FoodIngredient>();
	private FoodMaterialService foodMaterialService=(FoodMaterialService)Activator.getApplicationContext().getBean("foodMaterialService");
	//private FoodIngredientService foodIngredientService=(FoodIngredientService)Activator.getApplicationContext().getBean("foodIngredientService");
	private Table table_1;
	public ViewPartFoodMaterial() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_composite.heightHint = 33;
		composite.setLayoutData(gd_composite);
		
		Label lblPleaseKeyIn = new Label(composite, SWT.NONE);
		lblPleaseKeyIn.setText("Please key in food's name:");
		
		textFoodName = new Text(composite, SWT.BORDER);
		GridData gd_textFoodName = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_textFoodName.widthHint = 181;
		textFoodName.setLayoutData(gd_textFoodName);
		
		Button btnQuery = new Button(composite, SWT.NONE);
		btnQuery.setText("Query");
		btnQuery.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				if(!(textFoodName.getText().trim()).equals(""))
					foodMaterials=foodMaterialService.getFoodMaterials(textFoodName.getText().trim());
				else
					foodMaterials=foodMaterialService.getFoodMaterials(null);
				checkboxTableViewer.setInput(foodMaterials);
			}
			
		});
		
		Composite composite_1 = new Composite(parent, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Group grpFoodsList = new Group(composite_1, SWT.NONE);
		GridData gd_grpFoodsList = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grpFoodsList.widthHint = 563;
		grpFoodsList.setLayoutData(gd_grpFoodsList);
		grpFoodsList.setText("food's List");
		grpFoodsList.setLayout(new BorderLayout(0, 0));
		
		checkboxTableViewer = CheckboxTableViewer.newCheckList(grpFoodsList, SWT.BORDER | SWT.FULL_SELECTION);
		table = checkboxTableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(BorderLayout.CENTER);
		table.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				IStructuredSelection selection =(IStructuredSelection)checkboxTableViewer.getSelection();
				for(Iterator itr=selection.iterator();itr.hasNext();){
					FoodMaterial material=(FoodMaterial)itr.next();
					foodIngredients=material.getFoodIngredients();
					tableViewer.setInput(foodIngredients);
				}
			}
		});
		//foodMaterials=foodMaterialService.getFoodMaterialByMaterialName(null);
		checkboxTableViewer.setContentProvider(new ArrayContentProvider());
		//checkboxTableViewer.setInput(foodMaterials);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.setAlignment(SWT.CENTER);
		tblclmnNewColumn.setWidth(70);
		tblclmnNewColumn.setText("ID");
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("food's material id : "+((FoodMaterial)element).getId());
				}
				return new Integer(((FoodMaterial)element).getId()).toString();
			}
		});
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setWidth(160);
		tblclmnNewColumn_1.setText("Name");
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("food's material name : "+((FoodMaterial)element).getName());
				}
				return ((FoodMaterial)element).getName();
			}
			
		});
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(120);
		tblclmnNewColumn_2.setText("Alias Name");
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("food's material alias name : "+((FoodMaterial)element).getAlias());
				}
				return ((FoodMaterial)element).getAlias();
			}
			
		});
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(120);
		tblclmnNewColumn_3.setText("Category");
		tableViewerColumn_3.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				//FoodMaterial material=(FoodMaterial)element;
				if(logger.isDebugEnabled()){
					logger.debug("food's material categroy : "+((FoodMaterial)element).getFoodMaterialCategory().getName());
				}
				return ((FoodMaterial)element).getFoodMaterialCategory().getName();
			}
			
		});
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_4 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("Code");
		tableViewerColumn_4.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				//FoodMaterial material=(FoodMaterial)element;
				if(logger.isDebugEnabled()){
					logger.debug("food's material code : "+((FoodMaterial)element).getCode());
				}
				return ((FoodMaterial)element).getCode();
			}
		});
		
		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_2.setLayout(new FillLayout(SWT.VERTICAL));
		
		Group grpFoodMaterialsIngredition = new Group(composite_2, SWT.NONE);
		grpFoodMaterialsIngredition.setText("Food Material's Ingredient");
		grpFoodMaterialsIngredition.setLayout(new BorderLayout(0, 0));
		
		tableViewer = new TableViewer(grpFoodMaterialsIngredition, SWT.BORDER | SWT.FULL_SELECTION);
		table_1 = tableViewer.getTable();
		table_1.setHeaderVisible(true);
		table_1.setLinesVisible(true);
		tableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_5 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_5 = tableViewerColumn_5.getColumn();
		tblclmnNewColumn_5.setWidth(130);
		tblclmnNewColumn_5.setText("Property Name");
		tableViewerColumn_5.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("food's property name : "+((FoodIngredient)element).getFoodIngredientElement().getName());
				}
				return ((FoodIngredient)element).getFoodIngredientElement().getName();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_6 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_6 = tableViewerColumn_6.getColumn();
		tblclmnNewColumn_6.setWidth(130);
		tblclmnNewColumn_6.setText("Property Value");
		tableViewerColumn_6.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("food's property value : "+((FoodIngredient)element).getQty());
				}
				return ((FoodIngredient)element).getQty().toString();
			}
			
		});
		
		TableViewerColumn tableViewerColumn_7 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_7 = tableViewerColumn_7.getColumn();
		tblclmnNewColumn_7.setWidth(80);
		tblclmnNewColumn_7.setText("Unit");
		tableViewerColumn_7.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				if(logger.isDebugEnabled()){
					logger.debug("food's property unit : "+((FoodIngredient)element).getFoodIngredientElement().getUnit().getName());
				}
				return ((FoodIngredient)element).getFoodIngredientElement().getUnit().getName();
			}
			
		});
		Group grpFoodMaterialsPicture = new Group(composite_2, SWT.NONE);
		grpFoodMaterialsPicture.setText("Food Material's Picture");
		// TODO Auto-generated method stub

	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
}
